<template>
  <el-container class="main-container">
    <keep-alive>
      <Header :subtitle="subtitle" :header-height="headerHeight" :cover-name="coverName" />
    </keep-alive>
    <div class="main">
      <el-row>
        <div v-loading="listLoading" class="el-col-24 el-col-md-20 list">
          <div class="total">
            <span>Total：{{ total }}</span>
          </div>
          <div v-for="items in list" :key="items.key.year" class="list-items">
            <div class="list-year">
              <span>{{ items.key.year }}</span>
            </div>
            <el-row v-for="item in items.data" :key="item.id">
              <div class="list-item">
                <router-link :to="`/detail/${item.id}`">
                  <span class="title">{{ item.title }}</span>
                  <span class="date">{{ formatTime(item.createTime) }}</span>
                </router-link>
              </div>
            </el-row>
          </div>
        </div>
      </el-row>
      <el-row class="pagination">
        <button v-if="listQuery.page < pageCount" type="button" class="el-button next" @click="loadMore">
          Next
        </button>
      </el-row>
    </div>
    <el-footer>
      <Footer />
    </el-footer>
  </el-container>
</template>

<script type="text/ecmascript-6">
import Header from '../components/Header'
import Footer from '../components/Footer'
import { parseTime, arrGroupBy, addTime } from '../utils/index'
import { getList, getTagById, getCategoryById } from '../api/blog'

export default {
  components: {
    Header,
    Footer
  },
  data() {
    return {
      listLoading: true,
      subtitle: '',
      coverName: '/cover/cover5.jpg',
      headerHeight: 70,
      path: '',
      listQuery: {
        page: 1
      },
      list: [],
      listData: [],
      total: 0,
      pageCount: 0
    }
  },
  watch: {
    '$route'(to, from) {
      if (to.name === from.name && to.params !== from.params) {
        this.initData()
        this.fetchList()
      }
    }
  },
  created() {
    this.initData()
    this.fetchList()
  },
  methods: {
    fetchList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.listData = addTime(response.data.data)
        this.list = arrGroupBy(this.listData, ['year'])
        this.total = response.data.total
        this.pageCount = Math.ceil(this.total / 10)
        this.listLoading = false
      })
    },
    loadMore() {
      this.listLoading = true
      this.listQuery.page += 1
      getList(this.listQuery).then(response => {
        this.listData = this.listData.concat(addTime(response.data.data))
        this.list = arrGroupBy(this.listData, ['year'])
        this.listLoading = false
      })
    },
    formatTime(time) {
      return parseTime(time, '{m}-{d}')
    },
    initData() {
      this.path = this.$route.path
      if (this.path === '/archives') {
        this.subtitle = 'Archives'
        this.listQuery = { page: 1 }
      } else if (this.$route.name === 'tag') {
        getTagById(this.$route.params.tag).then(response => {
          document.title = response.data.title + " -- Rainfer's Blog"
          this.subtitle = 'Tag - ' + response.data.title
        })
        this.listQuery = {
          tag: this.$route.params.tag,
          page: 1
        }
      } else if (this.$route.name === 'category') {
        getCategoryById(this.$route.params.id).then(response => {
          document.title = response.data.title + " -- Rainfer's Blog"
          this.subtitle = 'Category - ' + response.data.title
        })
        this.listQuery = {
          cid: this.$route.params.id,
          page: 1
        }
      } else if (this.$route.name === 'search') {
        document.title = this.$route.params.kwd + " -- Rainfer's Blog"
        this.subtitle = 'Search - ' + this.$route.params.kwd
        this.listQuery = {
          title: this.$route.params.kwd,
          page: 1
        }
      }
    }
  }
}
</script>

<style scoped lang="scss" rel="stylesheet/scss">
    .main-container {
        background-color: #eeeeee;

        .main {
            max-width: 1140px;
            width: 100%;
            padding: 3rem 15px;
            margin-right: auto;
            margin-left: auto;
            position: relative;
            margin-top: -3rem;
            background-color: #fff;
            border-radius: .5rem;
            z-index: 3;
            -webkit-box-shadow: 0 12px 15px 0 rgba(0, 0, 0, .24), 0 17px 50px 0 rgba(0, 0, 0, .19) !important;
            box-shadow: 0 12px 15px 0 rgba(0, 0, 0, .24), 0 17px 50px 0 rgba(0, 0, 0, .19) !important;

            .list {
                float: none;
                margin: auto;

                .total {
                    font-size: 1.5rem;
                    font-weight: 600;
                    margin: 1.5rem auto;
                }

                .list-items {
                    .list-year {
                        font-size: 1.35rem;
                        font-weight: 600;
                        margin: .5rem auto;
                    }

                    .el-row {
                        &:nth-child(even) a {
                            background-color: #f5f6f7;
                        }
                    }

                    .list-item {
                        float: none;

                        a {
                            cursor: pointer;
                            text-decoration: none;
                            font-size: 1.25rem;
                            line-height: 3rem;
                            position: relative;
                            display: block;
                            background-color: #fff;
                            height: 3rem;
                            padding: 0 1.5rem;

                            &:hover, &:active {
                                z-index: 1;
                                text-decoration: none;
                                color: #212529;
                                background-color: #e9ecef;
                            }

                            .title {
                                white-space: nowrap;
                                overflow: hidden;
                                text-overflow: ellipsis;
                                float: left;
                            }

                            .date {
                                float: right;
                            }
                        }
                    }
                }
            }

            .pagination {
                text-align: center;
                margin-top: 2rem;

                .next {
                    -webkit-transition: all .3s linear;
                    -o-transition: all .3s linear;
                    transition: all .3s linear;
                    outline: 0;
                    border: 0;
                    background-color: transparent;
                    font-size: .9rem;
                    color: #212529;
                    padding: 0 10px;
                    display: inline-block;
                    line-height: 35px;

                    &:hover {
                        -webkit-transition: all .3s linear;
                        -o-transition: all .3s linear;
                        transition: all .3s linear;
                        -webkit-border-radius: .125rem;
                        border-radius: .125rem;
                        background-color: #eee;
                    }
                }
            }
        }
    }
</style>
